
#include "stdafx.h"
#include "WebPolicyDelegate.h"
#include "WebkitView.h"

HRESULT WebPolicyDelegate::QueryInterface(REFIID riid, void** ppvObject)
{
	*ppvObject = 0;
	if (IsEqualGUID(riid, IID_IUnknown))
		*ppvObject = static_cast<IWebPolicyDelegate*>(this);
	else if (IsEqualGUID(riid, IID_IWebPolicyDelegate))
		*ppvObject = static_cast<IWebPolicyDelegate*>(this);
	else
		return E_NOINTERFACE;

	AddRef();
	return S_OK;
}

ULONG WebPolicyDelegate::AddRef()
{
	return ++m_refCount;
}

ULONG WebPolicyDelegate::Release()
{
	ULONG newRef = --m_refCount;
	if (!newRef)
		delete(this);

	return newRef;
}

// IWebPolicyDelegate
HRESULT WebPolicyDelegate::decidePolicyForNavigationAction(IWebView* webView, IPropertyBag* actionInformation, IWebURLRequest* request, IWebFrame* frame, IWebPolicyDecisionListener* listener)
{
	if (!m_default) return E_FAIL;
	return m_default->decidePolicyForNavigationAction(webView, actionInformation, request, frame, listener);
}

HRESULT WebPolicyDelegate::decidePolicyForNewWindowAction(IWebView* webView, IPropertyBag* actionInformation, IWebURLRequest* request, BSTR frameName, IWebPolicyDecisionListener* listener)
{
	do {
		if (!request || !m_owner) break;
		//_bstr_t requestURL;
		//if (FAILED(request->URL(requestURL.GetAddress())) || requestURL.length() <= 0) break;
		m_owner->addWebPage(request, true);
	} while (0);
	if (listener) listener->ignore();
	return S_OK;
}

HRESULT WebPolicyDelegate::decidePolicyForMIMEType(IWebView* webView, BSTR type, IWebURLRequest* request, IWebFrame* frame, IWebPolicyDecisionListener* listener)
{
	if (!m_default) return E_FAIL;
	return m_default->decidePolicyForMIMEType(webView, type, request, frame, listener);
}

HRESULT WebPolicyDelegate::unableToImplementPolicyWithError(IWebView* webView, IWebError* error, IWebFrame* frame)
{
	if (!m_default) return E_FAIL;
	return m_default->unableToImplementPolicyWithError(webView, error, frame);
}
